﻿using Mota.XMLControls;
using System;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using EagleEyeCoreLibrary;

namespace Mota.EditModule
{
    public class EditFormGuiVisitor:GuiVisitor
    {
        private string mainTableName = String.Empty;
        private BindingSource mainBindingSource = null;
        private ConnectionConfigs connectionConfig;
        private DataSet mainDataSet = new DataSet();

        private EditFormGuiVisitor()
        {
        }

        public EditFormGuiVisitor(ConnectionConfigs configs, DataTable mainTable, out BindingSource binding)
        {
            connectionConfig = configs;
            mainTableName = mainTable.TableName;
            mainDataSet.Tables.Add(mainTable);
            mainBindingSource = new BindingSource() { DataSource = mainDataSet };
            mainBindingSource.DataMember = mainTableName;
            binding = mainBindingSource;
        }
        // fill with specific object data
        public void SetValue(String getXMLProc,String id)
        {
            var objectData = Model.GetObjectById(connectionConfig, 
                                                        getXMLProc,
                                                        id);
            DataTable dt = (mainBindingSource.DataSource as DataSet).Tables[mainTableName];
            // assign to specified columns object values
            DataRow dr = dt.Rows.Add();
            foreach (var data in objectData)
            {
                var columnIndex = dt.Columns.IndexOf(data.Key);
                if (columnIndex > -1)
                {
                    DataColumn dc = dt.Columns[columnIndex];
                    if (dc.DataType == typeof(Boolean))
                    {
                        // if true or false values
                        if (data.Value.ToString().Length > 1)
                            dr[dc] = Convert.ToBoolean(data.Value);
                        else //if 1 or 0 values
                            dr[dc] = Convert.ToByte(data.Value);
                    }
                    else if (dc.DataType == typeof(Byte[]))
                    {
                        dr[dc] = Convert.FromBase64String(data.Value.ToString());
                    }
                    else if (dc.DataType == typeof(Guid))
                    {
                        if (String.IsNullOrEmpty(data.Value.ToString()))
                            dr[dc] = DBNull.Value;
                        else
                            dr[dc] = data.Value;
                    }
                    else
                    {
                        dr[dc] = data.Value;
                    }
                }
#if MAT
                else
                    EventAppLogger.WriteAppWarning(Resources.GetString("{0} doesn't exist in related table {1} presentation",
                                        data.Key, mainTableName),connectionConfig.ConnectionString);
#endif
            }
        }

        public void OverrideDefaultValues(Dictionary<string,object> values)
        {
            try
            {

                if ((values != null) && (values.Count > 0))
                {
                    DataTable dt = (mainBindingSource.DataSource as DataSet).Tables[mainTableName];
                    // assign to specified columns object values
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        foreach (var data in values)
                        {
                            var columnIndex = dt.Columns.IndexOf(data.Key);

                            if (columnIndex > -1)
                            {
                                DataColumn dc = dt.Columns[columnIndex];
                                if (dc.DataType == typeof(Boolean))
                                {
                                    // if true or false values
                                    if (data.Value.ToString().Length > 1)
                                        dr[dc] = Convert.ToBoolean(data.Value);
                                    else //if 1 or 0 values
                                        dr[dc] = Convert.ToByte(data.Value);
                                }
                                else if (dc.DataType == typeof(Byte[]))
                                {
                                    dr[dc] = Convert.FromBase64String(data.Value.ToString());
                                }
                                else if (dc.DataType == typeof(Guid))
                                {
                                    if (String.IsNullOrEmpty(data.Value.ToString()))
                                        dr[dc] = DBNull.Value;
                                    else
                                        dr[dc] = data.Value;
                                }
                                else
                                {
                                    dr[dc] = data.Value;
                                }
                            }
                            else
                            {
                                throw new Exception(Resources.GetString("{0} doesn't exist in related table {1}", data.Key, mainTableName));
                            }
                        }
                    }
                }
            }catch(Exception ex)
            {
                ex.LogError("B7157AA8-44E3-47CB-A9F5-40601E415F84");
            }
        }

        public override void Visit(GuiComboBox guiCombobox)
        {
            guiCombobox.Refresh(connectionConfig,mainBindingSource);
        }

        public override void Visit(GuiTextBox guiTextBox)
        {
            guiTextBox.Refresh(mainBindingSource);
        }

        public override void Visit(GuiGroupBox guiGroupBox)
        {
        }

        public override void Visit(GuiLabel guiLabel)
        {
            guiLabel.Refresh(mainBindingSource);
        }

        public override void Visit(GuiSplitter guiSplitter)
        {
        }

        public override void Visit(GuiSplitterPanel guiSplitterPanel)
        {
        }

        public override void Visit(GuiTab guiTab)
        {
        }

        public override void Visit(GuiTabControl guiTabControl)
        {
        }

        public override void Visit(GuiWebBrowser guiWebBrowser)
        {
        }

        public override void Visit(GuiRichText guiRichText)
        {
            if (guiRichText.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiRichText.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiDateTime guiDateTime)
        {
            if (guiDateTime.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiDateTime.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiImageBox guiImageBox)
        {
            if (guiImageBox.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiImageBox.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiCheckBox guiCheckBox)
        {
            if (guiCheckBox.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiCheckBox.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiMaskText guiMaskText)
        {
            if (guiMaskText.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiMaskText.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiNumeric guiNumeric)
        {
            if (guiNumeric.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiNumeric.SetValue(mainBindingSource);
            }
        }
        public override void Visit(GuiCheckedList guiCheckedList)
        {
            guiCheckedList.Refresh(connectionConfig, mainBindingSource);
        }

        public override void Visit(GuiTreeView guiTreeView)
        {
            if (guiTreeView.XmlAttributes.ContainsKey("_ParentKeyField"))
            {
                guiTreeView.Refresh(connectionConfig, mainBindingSource);
            }
        }

        public override void Visit(GuiTime guiTime)
        {
            if (guiTime.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiTime.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiColor guiColor)
        {
            if (guiColor.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiColor.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiTrackBar guiTrackBar)
        {
            if (guiTrackBar.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                guiTrackBar.SetValue(mainBindingSource);
            }
        }

        public override void Visit(GuiRangeTrackBar guiRangeTrackBar)
        {
            if (guiRangeTrackBar.XmlAttributes.ContainsKey("XMLAttribute"))
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = mainDataSet;
                bs.DataMember = mainTableName;
                guiRangeTrackBar.Binding = bs;
                guiRangeTrackBar.SetValue(mainBindingSource);
            }
        }
        public override void Visit(GuiPanel guiPanel)
        {
            
        }

        public override void Visit(GuiCheckedComboBox guiCheckedComboBox)
        {
            guiCheckedComboBox.Refresh(connectionConfig, mainBindingSource);
        }

        public override void Visit(GuiListBox guiListBox)
        {
            guiListBox.Refresh(connectionConfig, mainBindingSource);
        }

        public override void Visit(GuiRadioGroup guiRadioGroup)
        {
            guiRadioGroup.Refresh(connectionConfig, mainBindingSource);
        }
       
    }
}
